package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.IBlackListBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.entity.BlackList;
import com.cloudservice.ccs.model.BlackListDetail;
import com.cloudservice.ccs.model.BlackListWithCustomerServiceNickName;
import com.cloudservice.ccs.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/black-list")
public class BlackListController {
    @Autowired
    private IBlackListBiz blackListBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody BlackList blackList) {
        // 接口问题待讨论：是否应当返回生成的 id？
        blackListBiz.save(blackList);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody BlackList blackList) {
        blackListBiz.update(id, blackList);
        BlackList result = blackListBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        blackListBiz.delete(id);
        return ApiResult.success();
    }

    @DeleteMapping("/{ids}/deletePartial")
    public ApiResult batchDelete(@PathVariable String ids) {
        Arrays.asList(ids.split(",")).forEach(idString -> {
            Integer id = Integer.parseInt(idString);
            blackListBiz.delete(id);
        });
        return ApiResult.success();
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        blackListBiz.partialUpdate(id, fields);
        BlackList result = blackListBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        BlackList result = blackListBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<BlackList> result = blackListBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<BlackList>) result));
    }

    @PostMapping("/with-customer-info")
    public ApiResult queryAllWithCustomerInfo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @RequestBody(required = false) Map<String, Object> conditions) {
        if (conditions == null) {
            conditions = new HashMap<>();
        }
        PageHelper.startPage(page, limit);
        List<BlackListDetail> result = blackListBiz.getAllWithCustomerInfo(conditions);
        return ApiResult.successWithData(PageData.fromPage((Page<BlackListDetail>) result));
    }

    @GetMapping("/customer/{customerId}")
    public ApiResult queryByCustomerId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerId) {
        PageHelper.startPage(page, limit);
        List<BlackListWithCustomerServiceNickName> result = blackListBiz.getByCustomerId(customerId);
        return ApiResult.successWithData(PageData.fromPage((Page<BlackListWithCustomerServiceNickName>) result));
    }
}
